module Explode.Cursor;
private import Explode.Stage;
private import Explode.Characters;
private import Explode.GameConfig;
private import Graphics.IGraphics;
private import Input.GamePad;
private import Input.Mouse;
private import System.Console;

public class EditCursor : IDrawingEntry {

	// ブロックの種類の数
	protected static const int KIND =12;

	protected int _x , _y , _z;
	protected bool _deadFlag , _fallFlag ,_ropeFlag , _ladderFlag;
	protected ICharacter _digger;
	protected BaseStage _stage;
	protected IGamePad _pad;
	protected IGraphics _graphics;
	protected string _textureName = "";
	protected ITexture _texture;
	protected ISprite _sprite;
	protected string _cpu;
	protected int _frameCnt;
	protected int _index;
	protected bool _visible;

	protected int _lastxm , _lastym; // PCモード用

	public float x() { return _x;}
	public float y() { return _y;}
	public float z() { return _z;}

	public void x( float v ) { _x = cast(int)v; }
	public void y( float v ) { _y = cast(int)v; }
	public void z( float v ) { _z = cast(int)v; }

	public int bx() { return _x >> Blocks.SHIFTX; }
	public int by() { return _y >> Blocks.SHIFTY; }

	public this( BaseStage stg , IGraphics g ) {
		Console.writeln("Create Cursor");
		_stage    = stg;
		_graphics = g;
		_pad = new VirtualGamePad();
		_sprite  = g.getSprite();
		reset();
	} //

	public void reset(){
		_x = Blocks.WIDTH ; _y = Blocks.HEIGHT ; _z = 180;
		_frameCnt = 0;

		if ( _textureName != _stage.playerTextureName ) {
			_textureName = _stage.mapTextureName;
			_texture	 = _graphics.createTexture( "MAPCHIP", _textureName , 0 , 0 );
			_texture.setFrameSize( 32 , 32 );
		}
		_visible = false;
		_texture.frameIndex = 4;
		_sprite.visible =  true;
		_sprite.texture = _texture;
		_sprite.setSize( Blocks.WIDTH , Blocks.HEIGHT );
		_sprite.moveTo( _x , _y );
		_sprite.z =  _z;
	} //

	public void padStatus( int st ){
		_pad.padStatus = st;
	} //

	public bool action() {
		_frameCnt++;
		return true;
	} //

	public bool drawingEntry( IGraphics g ) {
		if ( _visible )g.addDrawingEntry( this ) ;
		return true;
	} //

	public bool draw( IGraphics g ) {
		alias Blocks B;
		if ( _index > 0 ) {
			_texture.frameIndex = _index * 4;
			g.drawImage( _texture , _x , _y , B.WIDTH , B.HEIGHT , Color.create( 0xcc888888 ) );
			g.drawRect(             _x , _y , B.WIDTH , B.HEIGHT , Color.create( 0x88ffffff ) );
		} else 
			g.drawFilledRect(       _x , _y , B.WIDTH , B.HEIGHT , Color.create( 0x88ffffff ) );
		return true;
	} //

	public void setNextBlock() {
		_index = ( _index + 1 ) % KIND;
	} //
	public void setPreviousBlock() {
		_index = ( _index + ( KIND - 1 ) ) % KIND;
	} //
	public void setIndex( int index ) {
		_index = index;
	} //
	public int currentBlock() {
		return ( 1 << _index ) >> 1;
	} //

	public void  moveTo( int xx , int yy ) {
		_x = xx; _y = yy;
		if ( _x <= Blocks.WIDTH  ) _x = Blocks.WIDTH;
		if ( _x >= Blocks.WIDTH  * ( _stage.cols - 2 ) ) _x = Blocks.WIDTH  * ( _stage.cols - 2 );
		if ( _y <= Blocks.HEIGHT ) _y = Blocks.HEIGHT;
		if ( _y >= Blocks.HEIGHT * ( _stage.rows - 2 ) ) _y = Blocks.HEIGHT * ( _stage.rows - 2 );
		_visible = true;
	} //
	public void moveBy( int xx , int yy) {
		moveTo( _x + xx , _y + yy );
	} //

	public void moveRight() {
		_x += Blocks.WIDTH;
		if ( _x > Blocks.WIDTH  * ( _stage.cols - 2 ) ) _x = Blocks.WIDTH;
		_visible = true;
	} //
	public void moveLeft() {
		_x -= Blocks.WIDTH;
		if ( _x < Blocks.WIDTH  ) _x = Blocks.WIDTH  * ( _stage.cols - 2 );
		_visible = true;
	} //
	public void moveUp() {
		_y += Blocks.HEIGHT;
		if ( _y > Blocks.HEIGHT  * ( _stage.rows - 2 ) ) _y = Blocks.HEIGHT;
		_visible = true;
	} //
	public void moveDown() {
		_y -= Blocks.HEIGHT;
		if ( _y < Blocks.HEIGHT  ) _y = Blocks.HEIGHT  * ( _stage.rows - 2 );
		_visible = true;
	} //
	
	public void moveCenter() {
		moveTo( _stage.cols / 2 * Blocks.WIDTH , _stage.rows / 2 * Blocks.HEIGHT ) ;
		_visible = true;
	} //
	
} //



